<?php
class PagesAction extends CommonAction {
	public function _initialize(){
		parent::_initialize();
		C('TOKEN_ON', false);
	}
	function index(){
		
		$this->listmap="1 and site_id=".$this->siteid;
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and title like '%{$keywords}%'";
			$this->keywords=$keywords;
		}
		
		$relateSiteList = $this->relateSiteList;
		foreach($relateSiteList as $k => $v){
			$relateSiteList[$k]['pageslist'] = M('Pages') -> where(array('site_id' => $v['id'])) -> select();
		}
		$this->assign('relateSiteList', $relateSiteList);
		
        parent::_list();
		$this->display(); 
		
	}
	
	public function insert(){
		$id = $this -> dao -> data(array('site_id' => $this->siteid))->add();
		$this->success('添加成功，准备进入布局模块',U('edit', array('id' => $id)));
	}
	
	public function add(){
		$this->insert();
	}
	//删除页面
	public function delete(){
		M('Modules') -> where(array('pages_id' => array('in', $this->_get('id'))))->delete();
		parent::delete();
	}
	public function info(){
		$id = $this->_get('id');
		$info = $this -> dao -> where(array('id' => $id)) -> find();
		$this->cateList = M('Pagescate')->where(array('status' => 1))->select();
		$this->assign('info', $info); 
		$this->display();
	}
	public function edit(){ 
		$mdb = M('Modules_category');
		$modulesList = $mdb -> where(array('status' => 1)) -> select();
		$tmdb = M('Template_modules');
		foreach($modulesList as $k => $v){
		    $modulesList[$k]['mlist'] = $tmdb -> where(array('pid' => $v['id'], 'status' => 1)) -> select();
		}
		//可用模块列表
		$this->assign('modulesList', $modulesList);
		$id = $this->_get('id');
		$map['id'] = $id;
		$info = $this->dao->where($map) -> find();
		if(!$info){
			$this->error('页面不存在！');
		} 
		$this->initView();
		if(!empty($info['full_source'])){
			$info['full_source'] = $this -> dao -> getListToStyle($info['id'], $this->view).$this -> dao -> getListToString($info['id'], $this->view);
		}

		//dump($info['full_source']);
		$this->assign('info', $info);
		$this->assign('ModulesPaste',F('pasteList'));
		$this->display();
	}
	
	public function update(){
		
		if(isset($_POST['full_source']) && isset($_POST['full_source'])){
			$_POST['full_source'] = TMPLSTRIPSPACE($_POST['full_source']);
		    $_POST['source'] = TMPLSTRIPSPACE($_POST['source']);
		}
		
		parent::update();
		
	}
	
	
	//风格选择框
	public function pagesBox(){
		parent::_list();
		$this->display('Public:pagesBox');
	}
	//复制页面
	public function copy(){
		if($this->_get('iscopy') == 1){
			ob_start();
			if($this->_post('pages_id')){
				$pages_id = $_POST['pages_id'];
				$this -> dao -> copy($this->siteid,$this->_post('tositeid'), $pages_id);
				exit;
			}
			$this -> dao -> copy($this->_get('fsite_id'),$this->_get('tosite_id'));
		}
		if($this->_get('copyToSelf') == 1){
			ob_start();
			$this -> dao -> copy($this->siteid,$this->siteid, $this->_get('id'));
		}
	}
	public function showcopybox(){
		$this->pages_id = explode(',', $this->_get('pages_id'));
		$this->display();
	}
	//要复制站点的列表
	public function siteList(){
		$this->display();
	}
	
	//显示CSS编辑器
	public function cssedit(){
		$this->info = M('Modules') -> where(array('id' => $this->_get('modules_id'), 'site_id' => $this->siteid)) -> find();
		$medialist = getCssMedia($this->info['style']);
		$cssPathlist = getCssPath($this->info['style'], $this->_get('media'));
		
			$cssPathlist[] = $this->_get('cssPath');
			$cssPathlist[] = '.myBox{id}';
		
			$medialist[] = '@media screen and (max-width: 320px)';
			$medialist[] = '@media screen and (max-width: 480px)';
			$medialist[] = '@media screen and (max-width: 540px)';
			$medialist[] = '@media screen and (max-width: 640px)';
		
		
		$this->assign('medialist', array_unique($medialist));
		$this->assign('cssPathlist', array_unique($cssPathlist));
		
		$this->assign('css', getCss($this->info['style'], $this->_get('cssPath'), $this->_get('media')));
		$this->display();
	}
	
	//设置页面关联
	public function setRelate(){
		//检查权限
		if(!isset($_POST['data']) && $_POST['data'] == ''){
			$this -> error('请求数据有误！');
			exit;
		}
		$arr = explode('_', $_POST['data']);
		$this->dao -> where(array('site_id' => intval($arr[0]), 'relate_id' => $arr[2])) -> save(array('relate_id' => 0));
		$ret = $this->dao -> where(array('site_id' => intval($arr[0]), 'id' => $arr[1])) -> save(array('relate_id' => $arr[2]));
		if($ret !== false){
			$this->success('设置成功！');
		}else{
			$this->error($this->dao -> getError());
		}
	}
}